package form;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException;

import sun.font.EAttribute;

import dao.BaomatDAO;

import model.Baomat;
import model.User;

/**
 * Servlet implementation class Xulythemthongtinbaomat
 */
@WebServlet("/Xulythemthongtinbaomat")
public class Xulythemthongtinbaomat extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String emailbaove = "", sCMND = "", cmnd = "", sngaycap = "",
			noicap = "";
	private String err_changeprotect = "", finish_changeprotect;
	private Date ngaycap;
	private boolean check, check_err = true;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Xulythemthongtinbaomat() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			toDo(request, response);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			toDo(request, response);
		} catch (ClassNotFoundException | SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void toDo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException, ClassNotFoundException,
			SQLException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");

		emailbaove = request.getParameter("emailbaove");

		sCMND = request.getParameter("cmnd");
		if (tool.Xulychuoi.xulychuoi(sCMND) == true) {
			cmnd = sCMND;
		} else {
			err_changeprotect = "CMND bao gồm 9 số,không có số 0 ở đầu";
			request.setAttribute("err_changeprotect", err_changeprotect);
		}

		sngaycap = request.getParameter("ngaycap");
		try {
			ngaycap = Date.valueOf(sngaycap);
		} catch (IllegalArgumentException e) {
			err_changeprotect = "Nhắc lại! Không để trống các trường chứa dấu *";
			request.setAttribute("err_changeprotect", err_changeprotect);
		}
		noicap = request.getParameter("noicap");

		if (emailbaove == "" && noicap == "") {
			err_changeprotect = "Nhắc lại! Không để trống các trường chứa dấu *";
			request.setAttribute("err_changeprotect", err_changeprotect);
		}

		ArrayList<Baomat> arrBaomat = BaomatDAO.selectAll();
		for (Baomat listBaomat : arrBaomat) {
			if (listBaomat.getEmailbaove().equalsIgnoreCase(emailbaove)) {
				err_changeprotect = "Email bảo vệ đã tồn tại!Vui lòng chọn tên khác";
				request.setAttribute("err_changeprotect", err_changeprotect);
			}
		}

		Baomat baomat = new Baomat();
		baomat.setEmailbaove(emailbaove);
		baomat.setCmnd(cmnd);
		baomat.setNgaycap(ngaycap);
		baomat.setNoicap(noicap);
		System.out.println(baomat.toString());

		try {
			check = BaomatDAO.insert(baomat, user.getEmail());

		} catch (ClassNotFoundException
				| MySQLIntegrityConstraintViolationException e) {
			err_changeprotect = "Email bảo vệ đã tồn tại";
			request.setAttribute("err_changeprotect", err_changeprotect);

		}
		if (err_changeprotect.length() == 0) {
			check_err = false;
		}
		if (check_err == true) {
			RequestDispatcher rd = request.getServletContext()
					.getRequestDispatcher("/trangcanhan_thongtinbaomat.jsp");
			rd.forward(request, response);
		}
		if (check == true && check_err == false) {
			finish_changeprotect = "Cập nhật thông tin thành công";
			request.setAttribute("finish_changeprotect", finish_changeprotect);
			RequestDispatcher rd = request.getServletContext()
					.getRequestDispatcher("/trangcanhan_thongtinbaomat.jsp");
			rd.forward(request, response);
		}
	}
}
